package com.xiaoqiudong.forum.service.impl;

import com.xiaoqiudong.forum.common.AppResult;
import com.xiaoqiudong.forum.common.ResultCode;
import com.xiaoqiudong.forum.common.utils.StringUtils;
import com.xiaoqiudong.forum.dao.MessageMapper;
import com.xiaoqiudong.forum.exception.ApplicationException;
import com.xiaoqiudong.forum.model.Message;
import com.xiaoqiudong.forum.model.User;
import com.xiaoqiudong.forum.service.IMessageService;
import com.xiaoqiudong.forum.service.IUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Descroption
 * User:dell
 * Date:2025-08-29
 * Time:9:54
 */
@Service
@Slf4j
public class MessageServiceImpl implements IMessageService {
    @Resource
    private IUserService userService;
    @Resource
    private MessageMapper messageMapper;
    @Override
    public void create(Message message) {
        if (message == null || message.getPostUserId() == null
                || message.getReceiveUserId() == null || StringUtils.isEmpty(message.getContent())){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User receiveUser = userService.selectById(message.getReceiveUserId());
        //检验接受用户是否存在
        if (receiveUser == null || receiveUser.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //设置默认值
        message.setState((byte) 0); //表示未读状态
        message.setDeleteState((byte) 0);
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);

        //创建信息
        int row = messageMapper.insertSelective(message);
        if (row != 1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {

        //检验参数
        if (receiveUserId == null || receiveUserId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //检验用户是否存在
        User receiveUser = userService.selectById(receiveUserId);
        if (receiveUser == null || receiveUser.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        if (count == null){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        //检验参数
        if (receiveUserId == null || receiveUserId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //检验用户是否存在
        User receiveUser = userService.selectById(receiveUserId);
        if (receiveUser == null || receiveUser.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        if (id == null || id <= 0 || state < 0 || state > 2){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //构建更新对象
        Message message = new Message();
        message.setId(id);
        message.setState(state);
        Date date = new Date();
        message.setUpdateTime(date);

        int row = messageMapper.updateByPrimaryKeySelective(message);
        if (row != 1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }


    }

    @Override
    public Message selectById(Long id) {
        if (id == null || id <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;

    }

    @Override
    public void reply(Long replyId, Message message) {
        if (replyId == null || replyId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验对应的站内信状态
        Message existMessage = messageMapper.selectByPrimaryKey(replyId);
        if (existMessage == null || existMessage.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXIST.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_MESSAGE_NOT_EXIST));
        }
        //更新状态为已回复
        updateStateById(replyId,(byte) 2);
        //添加已回复的信息
        create(message);
    }
}
